package cn.com.se.多线程.ConcurrentUtils.CompletionServiceTest;

import java.util.concurrent.*;

/**
 * @Description TODO
 * @Author:hu.rl
 * @Date:2019-04-25 14:05
 * @Version 1.0
 **/
public class CompletionServiceTest {
    private static ExecutorService threadPool = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {
        CompletionService completionService = new ExecutorCompletionService(threadPool);
        for (int i = 0; i < 10; i++) {
            ConcurrentHashMap map = new ConcurrentHashMap();
            map.put("key" + i, "这是第" + i + "任务");
            System.out.println("这是第 " + i + "任务");
            completionService.submit(new CompletionTask(map));
        }

        for (int i = 0; i < 10; i++) {
            try {
                System.out.println("结果输出：" + completionService.take().get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}
